package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

@Api(tags = "检索list控制器")
@RestController
@RequestMapping("api/list")
public class ListApiController {
    @Autowired
    private ElasticsearchRestTemplate restTemplate;
    @Autowired
    private SearchService searchService;

    @ApiOperation(value = "利用es客户端手动建索引库")
    @GetMapping("inner/createIndex")
    public Result createIndex(){
        restTemplate.createIndex(Goods.class);
        restTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    @ApiOperation(value = "后台根据skuId商品上架")
    @GetMapping("inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable("skuId")Long skuId){
        searchService.upperGoods(skuId);
        return Result.ok();
    }

    @ApiOperation(value = "后台根据skuId商品下架")
    @GetMapping("inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable("skuId")Long skuId){
        searchService.lowerGoods(skuId);
        return Result.ok();
    }

    @ApiOperation(value = "浏览商品详情增加评分")
    @GetMapping("inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable("skuId")Long skuId){
        searchService.incrHotScore(skuId);
        return Result.ok();
    }

    @ApiOperation(value = "调用ES检索商品列表")
    @PostMapping
    public Result list(@RequestBody SearchParam searchParam){
        SearchResponseVo search = searchService.search(searchParam);
        return Result.ok(search);
    }
}
